import { Modal } from 'antd';
import {WeaLocaleProvider, WeaFormItem, WeaSearchGroup} from 'ecCom';
import {WeaSwitch} from 'comsMobx';
import _ from "lodash";
const getLabel = WeaLocaleProvider.getLabel;

export const transfStr = (name = '', ids = '', list = [], type = '') => {
	let str = '';
	const mirror = {
		37: "doc",
		prjtsk: "task",
		18: "crm",
		152: "workflow",
		135: "project",
		workplan: "workplan",
		blogTemplate: "blogTemplate"
	}
	list.map(item => {
		if(name === 'Upload' && type === 'image'){
			str += '<img class="formImgPlay" src="' + item.imgSrc + '" data-imgsrc="' + (item.loadlink || item.filelink) + '" />'
		}
		if(name === 'Upload' && type === 'file'){
			//str += (item.filelink + '<br>')
			str += (`<a href='javascript:void(0)' onclick='try{return downloads(${item.fileid},this);}catch(e){}' style='cursor:pointer;text-decoration:underline !important;margin-right:8px'>${item.filename}(${item.filesize})</a>&nbsp;<br/>`)
		}
		if(name === 'Browser'){
			str += (type === 'blogTemplate' ? item.name : `<a href='javascript:void(0)'  linkid='${item.id}' linkType='${mirror[type]}' onclick='try{return openAppLink(this,${item.id});}catch(e){}'  unselectable='off' contenteditable='false' style='cursor:pointer;text-decoration:underline !important;margin-right:8px'>${item.name || item.showname}</a>&nbsp;<br/>`)
		}
	})
	return str
}

export const templetChange = (valueNow = '' ,callback,  name = '', ids = '', list = [], type = '') => {
	if(valueNow){
		Modal.confirm({
			title: '替换当前内容？',
			onOk: () => {
				callback(transfStr(name, ids, list, type))
			},
			onCancel: () => {

			}
		});
	}else{
		callback(transfStr(name, ids, list, type))
	}
}

//带表单验证的form数据
export const getFormSearchs = (form, datas, needTigger = false, lableCol, fieldcol) => {
  const {isFormInit} = form;
  let group = [];
  const formParams = form.getFormParams();
  isFormInit && datas && datas.map((c,index) =>{
    let items = [];
    c.items.map(fields => {
      items.push({
        com:(
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@lk4pyu@${fields.domkey[0]}`}
            label={`${fields.label}`}
            labelCol={{span: lableCol ? lableCol : `${fields.labelcol}`}}
            wrapperCol={{span: fieldcol ? fieldcol : `${fields.fieldcol}`}}
            error={form.getError(fields)}
            tipPosition="bottom"
          >
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@jlnzgz@${fields.domkey[0]}`} fieldConfig={fields} form={form} formParams={formParams}/>
          </WeaFormItem>),
        colSpan:1
      })
    });
    if(needTigger){
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@o19k0u@${index}`} col={1} center needTigger={needTigger} title={c.title} showGroup={c.defaultshow} items={items}/>)
    }else {
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@y7vabg@${index}`} col={1} center needTigger={needTigger} showGroup={c.defaultshow} items={items}/>)
    }
  });
  return group;
}

//删除等信息确认
export const doConfirm = (content, func) => {//确认信息， 方法， 方法参数
  Modal.confirm({
    title: `${getLabel(131329,'信息确认')}`,
    content: content,
    okText: `${getLabel(382958,'确定')}`,
    cancelText:`${getLabel(31129,'取消')}`,
    onOk: () => func && func(),
  })
}

//提示
export const setModal = (content) => {
  Modal.warning({
    title: `${getLabel(15172,'系统提示')}`,
    content: content,
  });
}

//表单验证
export const  onCheck = (form, func) => {
  form.validateForm().then(f=>{
    if(f.isValid){
      func && typeof func == 'function' && func();
    }else{
      f.showErrors();
    }
  });
}

export const openWindow = url => {
	const width = screen.availWidth - 10;
	const height = screen.availHeight - 50;
	let szFeatures = 'top=0,';
	szFeatures += 'left=0,';
	szFeatures += `width=${width},`;
	szFeatures += `height=${height},`;
	szFeatures += 'directories=no,';
	szFeatures += 'status=yes,toolbar=no,location=no,';
	szFeatures += 'menubar=no,';
	szFeatures += 'scrollbars=yes,';
	szFeatures += 'resizable=yes';
	window.open(url, '', szFeatures);
};
window.hrefTaskInfo =  (id) => {window.open("/spa/govern/static/index.html#/main/govern/proinfo?id="+id)};
//window.hrefTaskInfo =  (id) => {window.open("/#/main/govern/proinfo?id="+id)};
//window.hrefTaskInfo =  (id) => {let key = Math.floor(Math.random() * 999999);window.open(`/spa/govern/static/index.html#/main/govern/proinfo?id=${id}&key=${key}`)};//lusx 19.03.28 360浏览器无法打开新标签页

//window.hrefTaskInfo =  (id) => {let key = Math.floor(Math.random() * 999999); window.open(`/#/main/govern/proinfo?id=${id}&key=${key}`)};
/**
 * 验证form接口是否返回正确数据
 * @param obj 接口返回的对象 obj data
 * @param conditionKey 接口返回的对象的key值  string condition1
 * @param itemName 接口返回的对象conditionKey对象里面的key值 string items
 */
export const isHasKey = (obj,conditionKey,itemName)=>{
		if(Object.keys(obj).length>1){
				if(Object.keys(obj[conditionKey][0].length>0)){
						if(_.has(obj[conditionKey][0],itemName)){
								if(obj[conditionKey][0][itemName].length>0){
									 return true;
								}else{
									return false;
								}
						}else{
							return false;
						}
				}else{
					return false;
				}
		}
};
export default window;
